package com.jf.cloud.service.sysmanager.dao.impl;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.jf.cloud.service.sysmanager.common.base.BaseService;
import com.jf.cloud.service.sysmanager.common.db.BaseDao;
import com.jf.cloud.service.sysmanager.common.util.DateUtil;
import com.jf.cloud.service.sysmanager.common.util.PinYinHelper;
import com.jf.cloud.service.sysmanager.dao.IPubgUserLoginBandingService;
import com.jf.cloud.service.sysmanager.dao.IPubgUserService;
import com.jf.cloud.service.sysmanager.entity.PubgUser;
import com.jf.cloud.service.sysmanager.entity.PubgUserLoginBanding;

/**
 * <p>
 * 其他登录方式绑定信息 服务实现类
 * </p>
 *
 * @author LangK
 * @since 2017-12-04
 */
@Service
public class PubgUserLoginBandingServiceImpl extends BaseService<PubgUserLoginBanding, Long> implements IPubgUserLoginBandingService {

	private static Logger log = LoggerFactory.getLogger(PubgUserLoginBandingServiceImpl.class);

	@Resource
	private IPubgUserService pubgUserService;

	/**
	 * JPA公共DAO
	 */
	@Resource
	private BaseDao baseDao;

	/**
	 * 第三方登陆调用
	 * 
	 * @author 朱建文 创建时间：2018年4月9日 上午11:19:31
	 */
	@Override
    @Transactional
    public Long register(String type, String openId, String name, String iconUrl, String sex, String city, String prvinice,String ip) {
		try {
			PubgUserLoginBanding userUserLoginBanding = new PubgUserLoginBanding();
			PubgUser pubgUser = new PubgUser();
			pubgUser.setNickname(name);
			pubgUser.setRegIp(ip);
			pubgUser.setRegTime(DateUtil.getCurrentDateTime());
			pubgUser.setPhoto(iconUrl);
			if ("0".equals(type)) {
			    userUserLoginBanding.setThirdTypeDesc("微信");
			    userUserLoginBanding.setThirdName("微信");
			} else {
			    userUserLoginBanding.setThirdTypeDesc("QQ");
			    userUserLoginBanding.setThirdName("QQ");
			}
			pubgUser = this.baseDao.saveAll(pubgUser);
			if (pubgUser.getRowId() <= 0) {
			    log.error("第三方登陆操作,插入用户信息时出错！" + openId);
			    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			    return -1L;
			}
			userUserLoginBanding.setUserId(pubgUser.getRowId());
			userUserLoginBanding.setBandingTime(DateUtil.getCurrentDateTime());
			userUserLoginBanding.setThirdId(openId);
			userUserLoginBanding.setThirdType(Integer.valueOf(type));
			userUserLoginBanding.setUserName(name);
			userUserLoginBanding = this.baseDao.saveAll(userUserLoginBanding);
			return userUserLoginBanding.getUserId();
		} catch (Exception e) {
			log.error("第三方登陆操作,插入用户第三方信息时出错", e);
			return -1L;
		}
    }
	
	/**
	 * 第三方登陆
	 * 
	 * @author 朱建文 创建时间：2018年5月9日 上午11:08:06
	 */
	public void insterUserLoginBanding(PubgUser pubgUser, PubgUserLoginBanding pubgUserLoginBanding) {
		try {
			
			// 生成昵称的拼音的首字母
			String nicknameInitial = pubgUser.getNickname().substring(0, 1);
			nicknameInitial = PinYinHelper.getFirstLetter(nicknameInitial, true);
			pubgUser.setNicknameInitial(nicknameInitial);
			
			// 保存用户对象
			pubgUser = this.baseDao.saveAll(pubgUser);

			// 保存用户的第三方登陆信息
			pubgUserLoginBanding.setUserId(pubgUser.getRowId());
			this.baseDao.saveAll(pubgUserLoginBanding);
		} catch (Exception e) {
			log.error("第三方登陆操作出错", e);
		}
	}
	
}
